<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('修改入库管理')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-stockEnter-edit" th:object="${wmsStockEnter}">
            <input name="enterId" th:field="*{enterId}" type="hidden">
            <input name="storageId" type="hidden" id="storageId"  th:field="*{storageId}"/>
        	<h4 class="form-header h4">入库信息</h4>
        	<div class="row">       
                <div class="col-sm-6">  
		            <div class="form-group">
		                <label class="col-sm-4 control-label is-required">标题：</label>
		                <div class="col-sm-8">
		                    <input name="title" class="form-control" type="text" th:field="*{title}" maxlength=50 required>
	                	</div>   	
		            </div> 
                </div>      
                <div class="col-sm-6">  
		            <div class="form-group">    
		                <label class="col-sm-4 control-label is-required">仓库：</label>
		                <div class="col-sm-8">
		                	<div class="input-group">
			                    <input name="storageName" id="storageName" th:field="*{storage.storageName}" onclick="selectStorage()" class="form-control" type="text" required readonly>
			                	<span class="input-group-addon"><i class="fa fa-search"></i></span>
							</div>
		                </div>
		            </div>
                </div> 
            </div>  
            <div class="row">       
                <div class="col-sm-6">  
		            <div class="form-group">    
		                <label class="col-sm-4 control-label is-required">入库日期：</label>
		                <div class="col-sm-8">
		                    <div class="input-group date">
		                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
		                        <input name="enterDate" th:value="${#dates.format(wmsStockEnter.enterDate, 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>
		                    </div>
		                </div>
		            </div>
                </div>      
                <div class="col-sm-6">              
		            <div class="form-group">    
		                <label class="col-sm-4 control-label is-required">负责人：</label>
		                <div class="col-sm-8">               
		                    <div class="input-group">
			                    <input name="enterBy" onclick="selectUser()" id="enterBy" type="text" th:field="*{enterBy}" placeholder="请选择负责人" class="form-control" required readonly>
			                	<span class="input-group-addon"><i class="fa fa-search"></i></span>
							</div>
		                </div>
		            </div>
                </div> 
            </div>  
            
            <h4 class="form-header h4">产品信息</h4>
            <div class="row">
                <div class="col-sm-12">
                    <button type="button" class="btn btn-success btn-sm" onclick="addColumn()"><i class="fa fa-plus"> 增加</i></button>
                    <button type="button" class="btn btn-danger btn-sm" onclick="sub.delColumn()"><i class="fa fa-minus"> 删除</i></button>
                    <div class="col-sm-12 select-table table-striped">
					    <table id="bootstrap-table"></table>
					</div>
                </div>
            </div>
        </form>
    </div>
    <div class="row">
        <div class="col-sm-offset-5 col-sm-10">
        	<button type="button" class="btn btn-sm btn-primary" onclick="submit()" shiro:hasPermission="crm:stockEnter:submit"><i class="fa fa-location-arrow"></i>提 交</button>&nbsp;
            <button type="button" class="btn btn-sm btn-success" onclick="submitHandler()"><i class="fa fa-check"></i>保 存</button>&nbsp;
            <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭 </button>
        </div>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script  th:inline="javascript">
        var prefix = ctx + "crm/stockEnter";
        var prefixStockProduct = ctx + "crm/stockProduct";
        $("#form-stockEnter-edit").validate({
            focusCleanup: true
        });
        
        function submit() {
        	//var enterId=[[${enterId}]]
        	$.modal.confirm("确认提交吗？", function() {
        		if ($.validate.form()) {
                	var data = $("#form-stockEnter-edit").serializeArray();  
                	if(data.length==6){
                		$.modal.msgWarning("请添加产品后再提交!");
                		return;
                	}
                    $.operate.saveTab(prefix + "/submit", data);
                }
        	});
            
        }

        function submitHandler() {
            if ($.validate.form()) {
            	var data = $("#form-stockEnter-edit").serializeArray();  
                $.operate.saveTab(prefix + "/edit", data);
            }
        }

        $("input[name='enterDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        }); 
        
        $(function() {
		    var options = {
			        url: prefixStockProduct + "/list",
			        height: $(window).height()/2,
		            queryParams:{
		            	'opId':[[${enterId}]],
		            	'type':0
		            },
	                pagination: false,
			        showSearch: false,
	                showRefresh: false,
	                showToggle: false,
	                showColumns: false,
	                sidePagination: "client",
			        columns: [{
			            checkbox: true
			        },
			        {
			        	field: 'index',
			        	align: 'center',
			        	title: "序号",
	                    formatter: function (value, row, index) {
	                    	var columnIndex = $.common.sprintf("<input type='hidden' name='index' value='%s'>", $.table.serialNumber(index));
	                    	return columnIndex + $.table.serialNumber(index);
	                    }
	                },
			        {
			            field: 'productId',
			            align: 'center',
			            title: '产品ID',
			            width:40,
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' name='products[%s].productId' id='productId_%s' value='%s' readonly required>", index, index, value);
			        		return html;
	                    }
			        },
			        {
			            field: 'productName',
			            align: 'center',
			            title: '产品名称',
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='text' placeholder='请选择产品' onclick='selectStockEnterProduct(" + index + ")' name='products[%s].productName' id='productName_%s' value='%s' required>", index, index, value);
			        		return html;
	                    }
			        },
			        {
			            field: 'productNum1',
			            align: 'center',
			            title: '入库数量',
			            formatter: function(value, row, index) {
			            	var html = $.common.sprintf("<input class='form-control' type='number' min=1 placeholder='请输入入库数量' name='products[%s].productNum1' value='%s' required>", index, value);
			        		return html;
	                    }
			        }]
			    };
			    $.table.init(options);
			});
	        
	        function addColumn() {
	        	var count = $("#" + table.options.id).bootstrapTable('getData').length;
	        	sub.editColumn();
	        	
	        	$("#" + table.options.id).bootstrapTable('insertRow', {
	        		index: count,
	                row: {
	                	index: $.table.serialNumber(count),
	                	productId:"",
	                	productName: "",
	                	productNum1: ""
	                }
	        	});
	        }

	        /*  选择仓库 */
	        function selectStorage() {
	        	 var url = ctx + "crm/storage/selectStorage";
	        	 $.modal.open("选择仓库", url);
	        }

	        /* 选择用户 */
	        function selectUser() {
	        	var url = ctx + 'system/user/selectUser';
	        	$.modal.open("选择人员", url);
	        }
	        
			// 选择入库产品
			function selectStockEnterProduct(columnId) {
	        	var url = ctx + "crm/product/selectStockEnterProduct/" + columnId;
				 $.modal.open("选择入库产品", url);
			}
    </script>
</body>
</html>